package com.lehuo.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import net.sf.json.JSONObject;

import com.lehuo.bean.User;

public class UserUtil {
	private static Statement statement = null;

	public static String getUserMsg(String username, String password,
			Connection connection) {
		User user = null;
		ResultSet rs = null;
		JSONObject jsonObject = null;
		String sql = "select * from user where username= '" + username + "'";
		try {
			connection = ConnUtil.getConnection();
			statement = (Statement) connection.createStatement();
			System.out.println("sql:" + sql);
			rs = statement.executeQuery(sql);
			if (rs.next()) {
				String mPassword = rs.getString("password");
				if (password.equals(mPassword)) {
					user = new User(rs.getInt("id") + "",
							rs.getString("nickname"),
							rs.getString("thumbnail"), "yes",
							rs.getString("tel"), rs.getString("password"));
				} else {
					user = new User("", "", "", "", "", "");
				}
			} else {
				user = new User("", "", "", "", "", "");
			}
			if (user != null) {
				jsonObject = JSONObject.fromObject(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return jsonObject.toString();
	}

	public static String register(String nickname, String username,
			String password, String thumbnail, Connection connection) {
		String register_sql = "insert into user(nickname,username,password,thumbnail)values('"
				+ nickname
				+ "','"
				+ username
				+ "','"
				+ password
				+ "','"
				+ thumbnail + "');";
		JSONObject jsonObject = new JSONObject();
		try {
			connection = ConnUtil.getConnection();
			statement = (Statement) connection.createStatement();
			System.out.println("register_sql:" + register_sql);
			int count = statement.executeUpdate(register_sql,
					Statement.RETURN_GENERATED_KEYS);
			ResultSet rs = statement.getGeneratedKeys();
			int key = -1;
			if (rs.next()) {
				key = rs.getInt(1);
				System.out.println(":" + key);
			}
			if (count > 0) {
				jsonObject.put("result", "success");
				jsonObject.put("id", key);
			} else {
				jsonObject.put("result", "error");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return jsonObject.toString();
	}

	public static String updataUser(String id, String nickname,
			String password, String thumbnail, String tel, Connection connection) {
		PreparedStatement pstmt = null;
		JSONObject jsonObject = new JSONObject();
		String updataUser_sql = "UPDATE user SET nickname= '" + nickname
				+ "',password= '" + password + "',thumbnail= '" + thumbnail
				+ "' ,tel='" + tel + "' where id= " + id;
		try {
			connection = ConnUtil.getConnection();
			System.out.println("updataUser_sql:" + updataUser_sql);
			pstmt = connection.prepareStatement(updataUser_sql);
			int num = pstmt.executeUpdate();
			if (num > 0) {
				jsonObject.put("result", "success");
			} else {
				jsonObject.put("result", "error");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return jsonObject.toString();
	}

	public static String getUserById(String id, Connection connection) {
		User user = null;
		ResultSet rs = null;
		JSONObject jsonObject = null;
		String sql = "select * from user where id= '" + id + "'";
		try {
			connection = ConnUtil.getConnection();
			statement = (Statement) connection.createStatement();
			System.out.println("sql:" + sql);
			rs = statement.executeQuery(sql);
			if (rs.next()) {
				user = new User(rs.getInt("id") + "", rs.getString("nickname"),
						rs.getString("thumbnail"), "yes", rs.getString("tel"),
						rs.getString("password"));
			} else {
				user = new User("", "", "", "", "","");
			}
			if (user != null) {
				jsonObject = JSONObject.fromObject(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return jsonObject.toString();
	}
}
